home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2002 November / CD 1 / APC0211D1.ISO / workshop / prog / files / ActivePerl-5.6.1.633-MSWin32.msi / _4eceb62e2203a0c9ed2573c78822ae5f < prev    next >
Encoding:
Text File  |  2001-09-04  |  47.2 KB  |  1,764 lines

  1. #!./miniperl
  2.  
  3. =head1 NAME
  4.  
  5. xsubpp - compiler to convert Perl XS code into C code
  6.  
  7. =head1 SYNOPSIS
  8.  
  9. B<xsubpp> [B<-v>] [B<-C++>] [B<-except>] [B<-s pattern>] [B<-prototypes>] [B<-noversioncheck>] [B<-nolinenumbers>] [B<-nooptimize>] [B<-typemap typemap>] ... file.xs
  10.  
  11. =head1 DESCRIPTION
  12.  
  13. This compiler is typically run by the makefiles created by L<ExtUtils::MakeMaker>.
  14.  
  15. I<xsubpp> will compile XS code into C code by embedding the constructs
  16. necessary to let C functions manipulate Perl values and creates the glue
  17. necessary to let Perl access those functions.  The compiler uses typemaps to
  18. determine how to map C function parameters and variables to Perl values.
  19.  
  20. The compiler will search for typemap files called I<typemap>.  It will use
  21. the following search path to find default typemaps, with the rightmost
  22. typemap taking precedence.
  23.  
  24.     ../../../typemap:../../typemap:../typemap:typemap
  25.  
  26. =head1 OPTIONS
  27.  
  28. Note that the C<XSOPT> MakeMaker option may be used to add these options to
  29. any makefiles generated by MakeMaker.
  30.  
  31. =over 5
  32.  
  33. =item B<-C++>
  34.  
  35. Adds ``extern "C"'' to the C code.
  36.  
  37. =item B<-except>
  38.  
  39. Adds exception handling stubs to the C code.
  40.  
  41. =item B<-typemap typemap>
  42.  
  43. Indicates that a user-supplied typemap should take precedence over the
  44. default typemaps.  This option may be used multiple times, with the last
  45. typemap having the highest precedence.
  46.  
  47. =item B<-v>
  48.  
  49. Prints the I<xsubpp> version number to standard output, then exits.
  50.  
  51. =item B<-prototypes>
  52.  
  53. By default I<xsubpp> will not automatically generate prototype code for
  54. all xsubs. This flag will enable prototypes.
  55.  
  56. =item B<-noversioncheck>
  57.  
  58. Disables the run time test that determines if the object file (derived
  59. from the C<.xs> file) and the C<.pm> files have the same version
  60. number.
  61.  
  62. =item B<-nolinenumbers>
  63.  
  64. Prevents the inclusion of `#line' directives in the output.
  65.  
  66. =item B<-nooptimize>
  67.  
  68. Disables certain optimizations.  The only optimization that is currently
  69. affected is the use of I<target>s by the output C code (see L<perlguts>).
  70. This may significantly slow down the generated code, but this is the way
  71. B<xsubpp> of 5.005 and earlier operated.
  72.  
  73. =item B<-noinout>
  74.  
  75. Disable recognition of C<IN>, C<OUT_LIST> and C<INOUT_LIST> declarations.
  76.  
  77. =item B<-noargtypes>
  78.  
  79. Disable recognition of ANSI-like descriptions of function signature.
  80.  
  81. =back
  82.  
  83. =head1 ENVIRONMENT
  84.  
  85. No environment variables are used.
  86.  
  87. =head1 AUTHOR
  88.  
  89. Larry Wall
  90.  
  91. =head1 MODIFICATION HISTORY
  92.  
  93. See the file F<changes.pod>.
  94.  
  95. =head1 SEE ALSO
  96.  
  97. perl(1), perlxs(1), perlxstut(1)
  98.  
  99. =cut
  100.  
  101. require 5.002;
  102. use Cwd;
  103. use vars '$cplusplus';
  104. use vars '%v';
  105.  
  106. use Config;
  107.  
  108. sub Q ;
  109.  
  110. # Global Constants
  111.  
  112. $XSUBPP_version = "1.9508";
  113.  
  114. my ($Is_VMS, $SymSet);
  115. if ($^O eq 'VMS') {
  116.     $Is_VMS = 1;
  117.     # Establish set of global symbols with max length 28, since xsubpp
  118.     # will later add the 'XS_' prefix.
  119.     require ExtUtils::XSSymSet;
  120.     $SymSet = new ExtUtils::XSSymSet 28;
  121. }
  122.  
  123. $FH = 'File0000' ;
  124.  
  125. $usage = "Usage: xsubpp [-v] [-C++] [-except] [-prototypes] [-noversioncheck] [-nolinenumbers] [-nooptimize] [-noinout] [-noargtypes] [-s pattern] [-typemap typemap]... file.xs\n";
  126.  
  127. $proto_re = "[" . quotemeta('\$%&*@;') . "]" ;
  128. # mjn
  129. $OBJ   = 1 if $Config{'ccflags'} =~ /PERL_OBJECT/i;
  130.  
  131. $except = "";
  132. $WantPrototypes = -1 ;
  133. $WantVersionChk = 1 ;
  134. $ProtoUsed = 0 ;
  135. $WantLineNumbers = 1 ;
  136. $WantOptimize = 1 ;
  137.  
  138. my $process_inout = 1;
  139. my $process_argtypes = 1;
  140.  
  141. SWITCH: while (@ARGV and $ARGV[0] =~ /^-./) {
  142.     $flag = shift @ARGV;
  143.     $flag =~ s/^-// ;
  144.     $spat = quotemeta shift,    next SWITCH    if $flag eq 's';
  145.     $cplusplus = 1,    next SWITCH    if $flag eq 'C++';
  146.     $WantPrototypes = 0, next SWITCH    if $flag eq 'noprototypes';
  147.     $WantPrototypes = 1, next SWITCH    if $flag eq 'prototypes';
  148.     $WantVersionChk = 0, next SWITCH    if $flag eq 'noversioncheck';
  149.     $WantVersionChk = 1, next SWITCH    if $flag eq 'versioncheck';
  150.     # XXX left this in for compat
  151.     $WantCAPI = 1, next SWITCH    if $flag eq 'object_capi';
  152.     $except = " TRY",    next SWITCH    if $flag eq 'except';
  153.     push(@tm,shift),    next SWITCH    if $flag eq 'typemap';
  154.     $WantLineNumbers = 0, next SWITCH    if $flag eq 'nolinenumbers';
  155.     $WantLineNumbers = 1, next SWITCH    if $flag eq 'linenumbers';
  156.     $WantOptimize = 0, next SWITCH    if $flag eq 'nooptimize';
  157.     $WantOptimize = 1, next SWITCH    if $flag eq 'optimize';
  158.     $process_inout = 0, next SWITCH    if $flag eq 'noinout';
  159.     $process_inout = 1, next SWITCH    if $flag eq 'inout';
  160.     $process_argtypes = 0, next SWITCH    if $flag eq 'noargtypes';
  161.     $process_argtypes = 1, next SWITCH    if $flag eq 'argtypes';
  162.     (print "xsubpp version $XSUBPP_version\n"), exit
  163.     if $flag eq 'v';
  164.     die $usage;
  165. }
  166. if ($WantPrototypes == -1)
  167.   { $WantPrototypes = 0}
  168. else
  169.   { $ProtoUsed = 1 }
  170.  
  171.  
  172. @ARGV == 1 or die $usage;
  173. ($dir, $filename) = $ARGV[0] =~ m#(.*)/(.*)#
  174.     or ($dir, $filename) = $ARGV[0] =~ m#(.*)\\(.*)#
  175.     or ($dir, $filename) = $ARGV[0] =~ m#(.*[>\]])(.*)#
  176.     or ($dir, $filename) = ('.', $ARGV[0]);
  177. chdir($dir);
  178. $pwd = cwd();
  179.  
  180. ++ $IncludedFiles{$ARGV[0]} ;
  181.  
  182. my(@XSStack) = ({type => 'none'});    # Stack of conditionals and INCLUDEs
  183. my($XSS_work_idx, $cpp_next_tmp) = (0, "XSubPPtmpAAAA");
  184.  
  185.  
  186. sub TrimWhitespace
  187. {
  188.     $_[0] =~ s/^\s+|\s+$//go ;
  189. }
  190.  
  191. sub TidyType
  192. {
  193.     local ($_) = @_ ;
  194.  
  195.     # rationalise any '*' by joining them into bunches and removing whitespace
  196.     s#\s*(\*+)\s*#$1#g;
  197.     s#(\*+)# $1 #g ;
  198.  
  199.     # change multiple whitespace into a single space
  200.     s/\s+/ /g ;
  201.     
  202.     # trim leading & trailing whitespace
  203.     TrimWhitespace($_) ;
  204.  
  205.     $_ ;
  206. }
  207.  
  208. $typemap = shift @ARGV;
  209. foreach $typemap (@tm) {
  210.     die "Can't find $typemap in $pwd\n" unless -r $typemap;
  211. }
  212. unshift @tm, qw(../../../../lib/ExtUtils/typemap ../../../lib/ExtUtils/typemap
  213.                 ../../lib/ExtUtils/typemap ../../../typemap ../../typemap
  214.                 ../typemap typemap);
  215. foreach $typemap (@tm) {
  216.     next unless -e $typemap ;
  217.     # skip directories, binary files etc.
  218.     warn("Warning: ignoring non-text typemap file '$typemap'\n"), next 
  219.     unless -T $typemap ;
  220.     open(TYPEMAP, $typemap) 
  221.     or warn ("Warning: could not open typemap file '$typemap': $!\n"), next;
  222.     $mode = 'Typemap';
  223.     $junk = "" ;
  224.     $current = \$junk;
  225.     while (<TYPEMAP>) {
  226.     next if /^\s*#/;
  227.         my $line_no = $. + 1; 
  228.     if (/^INPUT\s*$/)   { $mode = 'Input';   $current = \$junk;  next; }
  229.     if (/^OUTPUT\s*$/)  { $mode = 'Output';  $current = \$junk;  next; }
  230.     if (/^TYPEMAP\s*$/) { $mode = 'Typemap'; $current = \$junk;  next; }
  231.     if ($mode eq 'Typemap') {
  232.         chomp;
  233.         my $line = $_ ;
  234.             TrimWhitespace($_) ;
  235.         # skip blank lines and comment lines
  236.         next if /^$/ or /^#/ ;
  237.         my($type,$kind, $proto) = /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or
  238.         warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next;
  239.             $type = TidyType($type) ;
  240.         $type_kind{$type} = $kind ;
  241.             # prototype defaults to '$'
  242.             $proto = "\$" unless $proto ;
  243.             warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n") 
  244.                 unless ValidProtoString($proto) ;
  245.             $proto_letter{$type} = C_string($proto) ;
  246.     }
  247.     elsif (/^\s/) {
  248.         $$current .= $_;
  249.     }
  250.     elsif ($mode eq 'Input') {
  251.         s/\s+$//;
  252.         $input_expr{$_} = '';
  253.         $current = \$input_expr{$_};
  254.     }
  255.     else {
  256.         s/\s+$//;
  257.         $output_expr{$_} = '';
  258.         $current = \$output_expr{$_};
  259.     }
  260.     }
  261.     close(TYPEMAP);
  262. }
  263.  
  264. foreach $key (keys %input_expr) {
  265.     $input_expr{$key} =~ s/\n+$//;
  266. }
  267.  
  268. $bal = qr[(?:(?>[^()]+)|\((??{ $bal })\))*];    # ()-balanced
  269. $cast = qr[(?:\(\s*SV\s*\*\s*\)\s*)?];        # Optional (SV*) cast
  270. $size = qr[,\s* (??{ $bal }) ]x;        # Third arg (to setpvn)
  271.  
  272. foreach $key (keys %output_expr) {
  273.     use re 'eval';
  274.  
  275.     my ($t, $with_size, $arg, $sarg) =
  276.       ($output_expr{$key} =~
  277.      m[^ \s+ sv_set ( [iunp] ) v (n)?     # Type, is_setpvn
  278.          \s* \( \s* $cast \$arg \s* ,
  279.          \s* ( (??{ $bal }) )        # Set from
  280.          ( (??{ $size }) )?            # Possible sizeof set-from
  281.          \) \s* ; \s* $
  282.       ]x);
  283.     $targetable{$key} = [$t, $with_size, $arg, $sarg] if $t;
  284. }
  285.  
  286. $END = "!End!\n\n";        # "impossible" keyword (multiple newline)
  287.  
  288. # Match an XS keyword
  289. $BLOCK_re= '\s*(' . join('|', qw(
  290.     REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT 
  291.     CLEANUP ALIAS ATTRS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE
  292.     SCOPE INTERFACE INTERFACE_MACRO C_ARGS POSTCALL
  293.     )) . "|$END)\\s*:";
  294.  
  295. # Input:  ($_, @line) == unparsed input.
  296. # Output: ($_, @line) == (rest of line, following lines).
  297. # Return: the matched keyword if found, otherwise 0
  298. sub check_keyword {
  299.     $_ = shift(@line) while !/\S/ && @line;
  300.     s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2;
  301. }
  302.  
  303. my ($C_group_rex, $C_arg);
  304. # Group in C (no support for comments or literals)
  305. $C_group_rex = qr/ [({\[]
  306.            (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )*
  307.            [)}\]] /x ;
  308. # Chunk in C without comma at toplevel (no comments):
  309. $C_arg = qr/ (?: (?> [^()\[\]{},"']+ )
  310.          |   (??{ $C_group_rex })
  311.          |   " (?: (?> [^\\"]+ )
  312.            |   \\.
  313.            )* "        # String literal
  314.          |   ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal
  315.          )* /xs;
  316.  
  317. if ($WantLineNumbers) {
  318.     {
  319.     package xsubpp::counter;
  320.     sub TIEHANDLE {
  321.         my ($class, $cfile) = @_;
  322.         my $buf = "";
  323.         $SECTION_END_MARKER = "#line --- \"$cfile\"";
  324.         $line_no = 1;
  325.         bless \$buf;
  326.     }
  327.  
  328.     sub PRINT {
  329.         my $self = shift;
  330.         for (@_) {
  331.         $$self .= $_;
  332.         while ($$self =~ s/^([^\n]*\n)//) {
  333.             my $line = $1;
  334.             ++ $line_no;
  335.             $line =~ s|^\#line\s+---(?=\s)|#line $line_no|;
  336.             print STDOUT $line;
  337.         }
  338.         }
  339.     }
  340.  
  341.     sub PRINTF {
  342.         my $self = shift;
  343.         my $fmt = shift;
  344.         $self->PRINT(sprintf($fmt, @_));
  345.     }
  346.  
  347.     sub DESTROY {
  348.         # Not necessary if we're careful to end with a "\n"
  349.         my $self = shift;
  350.         print STDOUT $$self;
  351.     }
  352.     }
  353.  
  354.     my $cfile = $filename;
  355.     $cfile =~ s/\.xs$/.c/i or $cfile .= ".c";
  356.     tie(*PSEUDO_STDOUT, 'xsubpp::counter', $cfile);
  357.     select PSEUDO_STDOUT;
  358. }
  359.  
  360. sub print_section {
  361.     # the "do" is required for right semantics
  362.     do { $_ = shift(@line) } while !/\S/ && @line;
  363.     
  364.     print("#line ", $line_no[@line_no - @line -1], " \"$filename\"\n")
  365.     if $WantLineNumbers && !/^\s*#\s*line\b/ && !/^#if XSubPPtmp/;
  366.     for (;  defined($_) && !/^$BLOCK_re/o;  $_ = shift(@line)) {
  367.     print "$_\n";
  368.     }
  369.     print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers;
  370. }
  371.  
  372. sub merge_section {
  373.     my $in = '';
  374.   
  375.     while (!/\S/ && @line) {
  376.         $_ = shift(@line);
  377.     }
  378.     
  379.     for (;  defined($_) && !/^$BLOCK_re/o;  $_ = shift(@line)) {
  380.     $in .= "$_\n";
  381.     }
  382.     chomp $in;
  383.     return $in;
  384. }
  385.  
  386. sub process_keyword($)
  387. {
  388.     my($pattern) = @_ ;
  389.     my $kwd ;
  390.  
  391.     &{"${kwd}_handler"}() 
  392.         while $kwd = check_keyword($pattern) ;
  393. }
  394.  
  395. sub CASE_handler {
  396.     blurt ("Error: `CASE:' after unconditional `CASE:'")
  397.     if $condnum && $cond eq '';
  398.     $cond = $_;
  399.     TrimWhitespace($cond);
  400.     print "   ", ($condnum++ ? " else" : ""), ($cond ? " if ($cond)\n" : "\n");
  401.     $_ = '' ;
  402. }
  403.  
  404. sub INPUT_handler {
  405.     for (;  !/^$BLOCK_re/o;  $_ = shift(@line)) {
  406.     last if /^\s*NOT_IMPLEMENTED_YET/;
  407.     next unless /\S/;    # skip blank lines 
  408.  
  409.     TrimWhitespace($_) ;
  410.     my $line = $_ ;
  411.  
  412.     # remove trailing semicolon if no initialisation
  413.     s/\s*;$//g unless /[=;+].*\S/ ;
  414.  
  415.     # check for optional initialisation code
  416.     my $var_init = '' ;
  417.     $var_init = $1 if s/\s*([=;+].*)$//s ;
  418.     $var_init =~ s/"/\\"/g;
  419.  
  420.     s/\s+/ /g;
  421.     my ($var_type, $var_addr, $var_name) = /^(.*?[^&\s])\s*(\&?)\s*\b(\w+)$/s
  422.         or blurt("Error: invalid argument declaration '$line'"), next;
  423.  
  424.     # Check for duplicate definitions
  425.     blurt ("Error: duplicate definition of argument '$var_name' ignored"), next
  426.         if $arg_list{$var_name}++ 
  427.           or defined $arg_types{$var_name} and not $processing_arg_with_types;
  428.  
  429.     $thisdone |= $var_name eq "THIS";
  430.     $retvaldone |= $var_name eq "RETVAL";
  431.     $var_types{$var_name} = $var_type;
  432.     # XXXX This check is a safeguard against the unfinished conversion of
  433.     # generate_init().  When generate_init() is fixed,
  434.     # one can use 2-args map_type() unconditionally.
  435.     if ($var_type =~ / \( \s* \* \s* \) /x) {
  436.       # Function pointers are not yet supported with &output_init!
  437.       print "\t" . &map_type($var_type, $var_name);
  438.       $name_printed = 1;
  439.     } else {
  440.       print "\t" . &map_type($var_type);
  441.       $name_printed = 0;
  442.     }
  443.     $var_num = $args_match{$var_name};
  444.  
  445.         $proto_arg[$var_num] = ProtoString($var_type) 
  446.         if $var_num ;
  447.     $func_args =~ s/\b($var_name)\b/&$1/ if $var_addr;
  448.     if ($var_init =~ /^[=;]\s*NO_INIT\s*;?\s*$/
  449.         or $in_out{$var_name} and $in_out{$var_name} =~ /^OUT/
  450.         and $var_init !~ /\S/) {
  451.       if ($name_printed) {
  452.         print ";\n";
  453.       } else {
  454.         print "\t$var_name;\n";
  455.       }
  456.     } elsif ($var_init =~ /\S/) {
  457.         &output_init($var_type, $var_num, $var_name, $var_init, $name_printed);
  458.     } elsif ($var_num) {
  459.         # generate initialization code
  460.         &generate_init($var_type, $var_num, $var_name, $name_printed);
  461.     } else {
  462.         print ";\n";
  463.     }
  464.     }
  465. }
  466.  
  467. sub OUTPUT_handler {
  468.     for (;  !/^$BLOCK_re/o;  $_ = shift(@line)) {
  469.     next unless /\S/;
  470.     if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) {
  471.         $DoSetMagic = ($1 eq "ENABLE" ? 1 : 0);
  472.         next;
  473.     }
  474.     my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ;
  475.     blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next
  476.         if $outargs{$outarg} ++ ;
  477.     if (!$gotRETVAL and $outarg eq 'RETVAL') {
  478.         # deal with RETVAL last
  479.         $RETVAL_code = $outcode ;
  480.         $gotRETVAL = 1 ;
  481.         next ;
  482.     }
  483.     blurt ("Error: OUTPUT $outarg not an argument"), next
  484.         unless defined($args_match{$outarg});
  485.     blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next
  486.         unless defined $var_types{$outarg} ;
  487.     $var_num = $args_match{$outarg};
  488.     if ($outcode) {
  489.         print "\t$outcode\n";
  490.         print "\tSvSETMAGIC(ST(" , $var_num-1 , "));\n" if $DoSetMagic;
  491.     } else {
  492.         &generate_output($var_types{$outarg}, $var_num, $outarg, $DoSetMagic);
  493.     }
  494.     delete $in_out{$outarg}     # No need to auto-OUTPUT 
  495.       if exists $in_out{$outarg} and $in_out{$outarg} =~ /OUT$/;
  496.     }
  497. }
  498.  
  499. sub C_ARGS_handler() {
  500.     my $in = merge_section();
  501.   
  502.     TrimWhitespace($in);
  503.     $func_args = $in;
  504.  
  505. sub INTERFACE_MACRO_handler() {
  506.     my $in = merge_section();
  507.   
  508.     TrimWhitespace($in);
  509.     if ($in =~ /\s/) {        # two
  510.         ($interface_macro, $interface_macro_set) = split ' ', $in;
  511.     } else {
  512.         $interface_macro = $in;
  513.     $interface_macro_set = 'UNKNOWN_CVT'; # catch later
  514.     }
  515.     $interface = 1;        # local
  516.     $Interfaces = 1;        # global
  517. }
  518.  
  519. sub INTERFACE_handler() {
  520.     my $in = merge_section();
  521.   
  522.     TrimWhitespace($in);
  523.     
  524.     foreach (split /[\s,]+/, $in) {
  525.         $Interfaces{$_} = $_;
  526.     }
  527.     print Q<<"EOF";
  528. #    XSFUNCTION = $interface_macro($ret_type,cv,XSANY.any_dptr);
  529. EOF
  530.     $interface = 1;        # local
  531.     $Interfaces = 1;        # global
  532. }
  533.  
  534. sub CLEANUP_handler() { print_section() } 
  535. sub PREINIT_handler() { print_section() } 
  536. sub POSTCALL_handler() { print_section() } 
  537. sub INIT_handler()    { print_section() } 
  538.  
  539. sub GetAliases
  540. {
  541.     my ($line) = @_ ;
  542.     my ($orig) = $line ;
  543.     my ($alias) ;
  544.     my ($value) ;
  545.  
  546.     # Parse alias definitions
  547.     # format is
  548.     #    alias = value alias = value ...
  549.  
  550.     while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) {
  551.         $alias = $1 ;
  552.         $orig_alias = $alias ;
  553.         $value = $2 ;
  554.  
  555.         # check for optional package definition in the alias
  556.     $alias = $Packprefix . $alias if $alias !~ /::/ ;
  557.         
  558.         # check for duplicate alias name & duplicate value
  559.     Warn("Warning: Ignoring duplicate alias '$orig_alias'")
  560.         if defined $XsubAliases{$alias} ;
  561.  
  562.     Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values")
  563.         if $XsubAliasValues{$value} ;
  564.  
  565.     $XsubAliases = 1;
  566.     $XsubAliases{$alias} = $value ;
  567.     $XsubAliasValues{$value} = $orig_alias ;
  568.     }
  569.  
  570.     blurt("Error: Cannot parse ALIAS definitions from '$orig'")
  571.         if $line ;
  572. }
  573.  
  574. sub ATTRS_handler ()
  575. {
  576.     for (;  !/^$BLOCK_re/o;  $_ = shift(@line)) {
  577.     next unless /\S/;
  578.     TrimWhitespace($_) ;
  579.         push @Attributes, $_;
  580.     }
  581. }
  582.  
  583. sub ALIAS_handler ()
  584. {
  585.     for (;  !/^$BLOCK_re/o;  $_ = shift(@line)) {
  586.     next unless /\S/;
  587.     TrimWhitespace($_) ;
  588.         GetAliases($_) if $_ ;
  589.     }
  590. }
  591.  
  592. sub REQUIRE_handler ()
  593. {
  594.     # the rest of the current line should contain a version number
  595.     my ($Ver) = $_ ;
  596.  
  597.     TrimWhitespace($Ver) ;
  598.  
  599.     death ("Error: REQUIRE expects a version number")
  600.     unless $Ver ;
  601.  
  602.     # check that the version number is of the form n.n
  603.     death ("Error: REQUIRE: expected a number, got '$Ver'")
  604.     unless $Ver =~ /^\d+(\.\d*)?/ ;
  605.  
  606.     death ("Error: xsubpp $Ver (or better) required--this is only $XSUBPP_version.")
  607.         unless $XSUBPP_version >= $Ver ; 
  608. }
  609.  
  610. sub VERSIONCHECK_handler ()
  611. {
  612.     # the rest of the current line should contain either ENABLE or
  613.     # DISABLE
  614.  
  615.     TrimWhitespace($_) ;
  616.  
  617.     # check for ENABLE/DISABLE
  618.     death ("Error: VERSIONCHECK: ENABLE/DISABLE")
  619.         unless /^(ENABLE|DISABLE)/i ;
  620.  
  621.     $WantVersionChk = 1 if $1 eq 'ENABLE' ;
  622.     $WantVersionChk = 0 if $1 eq 'DISABLE' ;
  623.  
  624. }
  625.  
  626. sub PROTOTYPE_handler ()
  627. {
  628.     my $specified ;
  629.  
  630.     death("Error: Only 1 PROTOTYPE definition allowed per xsub") 
  631.         if $proto_in_this_xsub ++ ;
  632.  
  633.     for (;  !/^$BLOCK_re/o;  $_ = shift(@line)) {
  634.     next unless /\S/;
  635.     $specified = 1 ;
  636.     TrimWhitespace($_) ;
  637.         if ($_ eq 'DISABLE') {
  638.        $ProtoThisXSUB = 0 
  639.         }
  640.         elsif ($_ eq 'ENABLE') {
  641.        $ProtoThisXSUB = 1 
  642.         }
  643.         else {
  644.             # remove any whitespace
  645.             s/\s+//g ;
  646.             death("Error: Invalid prototype '$_'")
  647.                 unless ValidProtoString($_) ;
  648.             $ProtoThisXSUB = C_string($_) ;
  649.         }
  650.     }
  651.  
  652.     # If no prototype specified, then assume empty prototype ""
  653.     $ProtoThisXSUB = 2 unless $specified ;
  654.  
  655.     $ProtoUsed = 1 ;
  656.  
  657. }
  658.  
  659. sub SCOPE_handler ()
  660. {
  661.     death("Error: Only 1 SCOPE declaration allowed per xsub") 
  662.         if $scope_in_this_xsub ++ ;
  663.  
  664.     for (;  !/^$BLOCK_re/o;  $_ = shift(@line)) {
  665.         next unless /\S/;
  666.         TrimWhitespace($_) ;
  667.         if ($_ =~ /^DISABLE/i) {
  668.            $ScopeThisXSUB = 0 
  669.         }
  670.         elsif ($_ =~ /^ENABLE/i) {
  671.            $ScopeThisXSUB = 1 
  672.         }
  673.     }
  674.  
  675. }
  676.  
  677. sub PROTOTYPES_handler ()
  678. {
  679.     # the rest of the current line should contain either ENABLE or
  680.     # DISABLE 
  681.  
  682.     TrimWhitespace($_) ;
  683.  
  684.     # check for ENABLE/DISABLE
  685.     death ("Error: PROTOTYPES: ENABLE/DISABLE")
  686.         unless /^(ENABLE|DISABLE)/i ;
  687.  
  688.     $WantPrototypes = 1 if $1 eq 'ENABLE' ;
  689.     $WantPrototypes = 0 if $1 eq 'DISABLE' ;
  690.     $ProtoUsed = 1 ;
  691.  
  692. }
  693.  
  694. sub INCLUDE_handler ()
  695. {
  696.     # the rest of the current line should contain a valid filename
  697.  
  698.     TrimWhitespace($_) ;
  699.  
  700.     death("INCLUDE: filename missing")
  701.         unless $_ ;
  702.  
  703.     death("INCLUDE: output pipe is illegal")
  704.         if /^\s*\|/ ;
  705.  
  706.     # simple minded recursion detector
  707.     death("INCLUDE loop detected")
  708.         if $IncludedFiles{$_} ;
  709.  
  710.     ++ $IncludedFiles{$_} unless /\|\s*$/ ;
  711.  
  712.     # Save the current file context.
  713.     push(@XSStack, {
  714.     type        => 'file',
  715.         LastLine        => $lastline,
  716.         LastLineNo      => $lastline_no,
  717.         Line            => \@line,
  718.         LineNo          => \@line_no,
  719.         Filename        => $filename,
  720.         Handle          => $FH,
  721.         }) ;
  722.  
  723.     ++ $FH ;
  724.  
  725.     # open the new file
  726.     open ($FH, "$_") or death("Cannot open '$_': $!") ;
  727.  
  728.     print Q<<"EOF" ;
  729. #
  730. #/* INCLUDE:  Including '$_' from '$filename' */
  731. #
  732. EOF
  733.  
  734.     $filename = $_ ;
  735.  
  736.     # Prime the pump by reading the first 
  737.     # non-blank line
  738.  
  739.     # skip leading blank lines
  740.     while (<$FH>) {
  741.         last unless /^\s*$/ ;
  742.     }
  743.  
  744.     $lastline = $_ ;
  745.     $lastline_no = $. ;
  746.  
  747. }
  748.  
  749. sub PopFile()
  750. {
  751.     return 0 unless $XSStack[-1]{type} eq 'file' ;
  752.  
  753.     my $data     = pop @XSStack ;
  754.     my $ThisFile = $filename ;
  755.     my $isPipe   = ($filename =~ /\|\s*$/) ;
  756.  
  757.     -- $IncludedFiles{$filename}
  758.         unless $isPipe ;
  759.  
  760.     close $FH ;
  761.  
  762.     $FH         = $data->{Handle} ;
  763.     $filename   = $data->{Filename} ;
  764.     $lastline   = $data->{LastLine} ;
  765.     $lastline_no = $data->{LastLineNo} ;
  766.     @line       = @{ $data->{Line} } ;
  767.     @line_no    = @{ $data->{LineNo} } ;
  768.  
  769.     if ($isPipe and $? ) {
  770.         -- $lastline_no ;
  771.         print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n"  ;
  772.         exit 1 ;
  773.     }
  774.  
  775.     print Q<<"EOF" ;
  776. #
  777. #/* INCLUDE: Returning to '$filename' from '$ThisFile' */
  778. #
  779. EOF
  780.  
  781.     return 1 ;
  782. }
  783.  
  784. sub ValidProtoString ($)
  785. {
  786.     my($string) = @_ ;
  787.  
  788.     if ( $string =~ /^$proto_re+$/ ) {
  789.         return $string ;
  790.     }
  791.  
  792.     return 0 ;
  793. }
  794.  
  795. sub C_string ($)
  796. {
  797.     my($string) = @_ ;
  798.  
  799.     $string =~ s[\\][\\\\]g ;
  800.     $string ;
  801. }
  802.  
  803. sub ProtoString ($)
  804. {
  805.     my ($type) = @_ ;
  806.  
  807.     $proto_letter{$type} or "\$" ;
  808. }
  809.  
  810. sub check_cpp {
  811.     my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line);
  812.     if (@cpp) {
  813.     my ($cpp, $cpplevel);
  814.     for $cpp (@cpp) {
  815.         if ($cpp =~ /^\#\s*if/) {
  816.         $cpplevel++;
  817.         } elsif (!$cpplevel) {
  818.         Warn("Warning: #else/elif/endif without #if in this function");
  819.         print STDERR "    (precede it with a blank line if the matching #if is outside the function)\n"
  820.             if $XSStack[-1]{type} eq 'if';
  821.         return;
  822.         } elsif ($cpp =~ /^\#\s*endif/) {
  823.         $cpplevel--;
  824.         }
  825.     }
  826.     Warn("Warning: #if without #endif in this function") if $cpplevel;
  827.     }
  828. }
  829.  
  830.  
  831. sub Q {
  832.     my($text) = @_;
  833.     $text =~ s/^#//gm;
  834.     $text =~ s/\[\[/{/g;
  835.     $text =~ s/\]\]/}/g;
  836.     $text;
  837. }
  838.  
  839. open($FH, $filename) or die "cannot open $filename: $!\n";
  840.  
  841. # Identify the version of xsubpp used
  842. print <<EOM ;
  843. /*
  844.  * This file was generated automatically by xsubpp version $XSUBPP_version from the 
  845.  * contents of $filename. Do not edit this file, edit $filename instead.
  846.  *
  847.  *    ANY CHANGES MADE HERE WILL BE LOST! 
  848.  *
  849.  */
  850.  
  851. EOM
  852.  
  853.  
  854. print("#line 1 \"$filename\"\n")
  855.     if $WantLineNumbers;
  856.  
  857. firstmodule:
  858. while (<$FH>) {
  859.     if (/^=/) {
  860.         my $podstartline = $.;
  861.         do {
  862.         if (/^=cut\s*$/) {
  863.         print("/* Skipped embedded POD. */\n");
  864.         printf("#line %d \"$filename\"\n", $. + 1)
  865.           if $WantLineNumbers;
  866.         next firstmodule
  867.         }
  868.  
  869.     } while (<$FH>);
  870.     # At this point $. is at end of file so die won't state the start
  871.     # of the problem, and as we haven't yet read any lines &death won't
  872.     # show the correct line in the message either.
  873.     die ("Error: Unterminated pod in $filename, line $podstartline\n")
  874.       unless $lastline;
  875.     }
  876.     last if ($Module, $Package, $Prefix) =
  877.     /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/;
  878.  
  879.     if ($OBJ) {
  880.         s/#if(?:def\s|\s+defined)\s*(\(__cplusplus\)|__cplusplus)/#if defined(__cplusplus) && !defined(PERL_OBJECT)/;
  881.     }
  882.     print $_;
  883. }
  884. &Exit unless defined $_;
  885.  
  886. print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers;
  887.  
  888. $lastline    = $_;
  889. $lastline_no = $.;
  890.  
  891. # Read next xsub into @line from ($lastline, <$FH>).
  892. sub fetch_para {
  893.     # parse paragraph
  894.     death ("Error: Unterminated `#if/#ifdef/#ifndef'")
  895.     if !defined $lastline && $XSStack[-1]{type} eq 'if';
  896.     @line = ();
  897.     @line_no = () ;
  898.     return PopFile() if !defined $lastline;
  899.  
  900.     if ($lastline =~
  901.     /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) {
  902.     $Module = $1;
  903.     $Package = defined($2) ? $2 : '';    # keep -w happy
  904.     $Prefix  = defined($3) ? $3 : '';    # keep -w happy
  905.     $Prefix = quotemeta $Prefix ;
  906.     ($Module_cname = $Module) =~ s/\W/_/g;
  907.     ($Packid = $Package) =~ tr/:/_/;
  908.     $Packprefix = $Package;
  909.     $Packprefix .= "::" if $Packprefix ne "";
  910.     $lastline = "";
  911.     }
  912.  
  913.     for(;;) {
  914.     # Skip embedded PODs 
  915.     while ($lastline =~ /^=/) {
  916.             while ($lastline = <$FH>) {
  917.             last if ($lastline =~ /^=cut\s*$/);
  918.         }
  919.         death ("Error: Unterminated pod") unless $lastline;
  920.         $lastline = <$FH>;
  921.         chomp $lastline;
  922.         $lastline =~ s/^\s+$//;
  923.     }
  924.     if ($lastline !~ /^\s*#/ ||
  925.         # CPP directives:
  926.         #    ANSI:    if ifdef ifndef elif else endif define undef
  927.         #        line error pragma
  928.         #    gcc:    warning include_next
  929.         #   obj-c:    import
  930.         #   others:    ident (gcc notes that some cpps have this one)
  931.         $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) {
  932.         last if $lastline =~ /^\S/ && @line && $line[-1] eq "";
  933.         push(@line, $lastline);
  934.         push(@line_no, $lastline_no) ;
  935.     }
  936.  
  937.     # Read next line and continuation lines
  938.     last unless defined($lastline = <$FH>);
  939.     $lastline_no = $.;
  940.     my $tmp_line;
  941.     $lastline .= $tmp_line
  942.         while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>));
  943.  
  944.     chomp $lastline;
  945.     $lastline =~ s/^\s+$//;
  946.     }
  947.     pop(@line), pop(@line_no) while @line && $line[-1] eq "";
  948.     1;
  949. }
  950.  
  951. PARAGRAPH:
  952. while (fetch_para()) {
  953.     # Print initial preprocessor statements and blank lines
  954.     while (@line && $line[0] !~ /^[^\#]/) {
  955.     my $line = shift(@line);
  956.     print $line, "\n";
  957.     next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/;
  958.     my $statement = $+;
  959.     if ($statement eq 'if') {
  960.         $XSS_work_idx = @XSStack;
  961.         push(@XSStack, {type => 'if'});
  962.     } else {
  963.         death ("Error: `$statement' with no matching `if'")
  964.         if $XSStack[-1]{type} ne 'if';
  965.         if ($XSStack[-1]{varname}) {
  966.         push(@InitFileCode, "#endif\n");
  967.         push(@BootCode,     "#endif");
  968.         }
  969.  
  970.         my(@fns) = keys %{$XSStack[-1]{functions}};
  971.         if ($statement ne 'endif') {
  972.         # Hide the functions defined in other #if branches, and reset.
  973.         @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns;
  974.         @{$XSStack[-1]}{qw(varname functions)} = ('', {});
  975.         } else {
  976.         my($tmp) = pop(@XSStack);
  977.         0 while (--$XSS_work_idx
  978.              && $XSStack[$XSS_work_idx]{type} ne 'if');
  979.         # Keep all new defined functions
  980.         push(@fns, keys %{$tmp->{other_functions}});
  981.         @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns;
  982.         }
  983.     }
  984.     }
  985.  
  986.     next PARAGRAPH unless @line;
  987.  
  988.     if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) {
  989.     # We are inside an #if, but have not yet #defined its xsubpp variable.
  990.     print "#define $cpp_next_tmp 1\n\n";
  991.     push(@InitFileCode, "#if $cpp_next_tmp\n");
  992.     push(@BootCode,     "#if $cpp_next_tmp");
  993.     $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++;
  994.     }
  995.  
  996.     death ("Code is not inside a function"
  997.        ." (maybe last function was ended by a blank line "
  998.        ." followed by a a statement on column one?)")
  999.     if $line[0] =~ /^\s/;
  1000.  
  1001.     # initialize info arrays
  1002.     undef(%args_match);
  1003.     undef(%var_types);
  1004.     undef(%defaults);
  1005.     undef($class);
  1006.     undef($static);
  1007.     undef($elipsis);
  1008.     undef($wantRETVAL) ;
  1009.     undef($RETVAL_no_return) ;
  1010.     undef(%arg_list) ;
  1011.     undef(@proto_arg) ;
  1012.     undef(@arg_with_types) ;
  1013.     undef($processing_arg_with_types) ;
  1014.     undef(%arg_types) ;
  1015.     undef(@outlist) ;
  1016.     undef(%in_out) ;
  1017.     undef($proto_in_this_xsub) ;
  1018.     undef($scope_in_this_xsub) ;
  1019.     undef($interface);
  1020.     undef($prepush_done);
  1021.     $interface_macro = 'XSINTERFACE_FUNC' ;
  1022.     $interface_macro_set = 'XSINTERFACE_FUNC_SET' ;
  1023.     $ProtoThisXSUB = $WantPrototypes ;
  1024.     $ScopeThisXSUB = 0;
  1025.     $xsreturn = 0;
  1026.  
  1027.     $_ = shift(@line);
  1028.     while ($kwd = check_keyword("REQUIRE|PROTOTYPES|VERSIONCHECK|INCLUDE")) {
  1029.         &{"${kwd}_handler"}() ;
  1030.         next PARAGRAPH unless @line ;
  1031.         $_ = shift(@line);
  1032.     }
  1033.  
  1034.     if (check_keyword("BOOT")) {
  1035.     &check_cpp;
  1036.     push (@BootCode, "#line $line_no[@line_no - @line] \"$filename\"")
  1037.       if $WantLineNumbers && $line[0] !~ /^\s*#\s*line\b/;
  1038.         push (@BootCode, @line, "") ;
  1039.         next PARAGRAPH ;
  1040.     }
  1041.  
  1042.  
  1043.     # extract return type, function name and arguments
  1044.     ($ret_type) = TidyType($_);
  1045.     $RETVAL_no_return = 1 if $ret_type =~ s/^NO_OUTPUT\s+//;
  1046.  
  1047.     # Allow one-line ANSI-like declaration
  1048.     unshift @line, $2
  1049.       if $process_argtypes
  1050.     and $ret_type =~ s/^(.*?\w.*?)\s*\b(\w+\s*\(.*)/$1/s;
  1051.  
  1052.     # a function definition needs at least 2 lines
  1053.     blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH
  1054.     unless @line ;
  1055.  
  1056.     $static = 1 if $ret_type =~ s/^static\s+//;
  1057.  
  1058.     $func_header = shift(@line);
  1059.     blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH
  1060.     unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s;
  1061.  
  1062.     ($class, $func_name, $orig_args) =  ($1, $2, $3) ;
  1063.     $class = "$4 $class" if $4;
  1064.     ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/;
  1065.     ($clean_func_name = $func_name) =~ s/^$Prefix//;
  1066.     $Full_func_name = "${Packid}_$clean_func_name";
  1067.     if ($Is_VMS) { $Full_func_name = $SymSet->addsym($Full_func_name); }
  1068.  
  1069.     # Check for duplicate function definition
  1070.     for $tmp (@XSStack) {
  1071.     next unless defined $tmp->{functions}{$Full_func_name};
  1072.     Warn("Warning: duplicate function definition '$clean_func_name' detected");
  1073.     last;
  1074.     }
  1075.     $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ;
  1076.     %XsubAliases = %XsubAliasValues = %Interfaces = @Attributes = ();
  1077.     $DoSetMagic = 1;
  1078.  
  1079.     $orig_args =~ s/\\\s*/ /g;        # process line continuations
  1080.  
  1081.     my %only_outlist;
  1082.     if ($process_argtypes and $orig_args =~ /\S/) {
  1083.     my $args = "$orig_args ,";
  1084.     if ($args =~ /^( (??{ $C_arg }) , )* $ /x) {
  1085.         @args = ($args =~ /\G ( (??{ $C_arg }) ) , /xg);
  1086.         for ( @args ) {
  1087.         s/^\s+//;
  1088.         s/\s+$//;
  1089.         my $arg = $_;
  1090.         my $default;
  1091.         ($arg, $default) = / ( [^=]* ) ( (?: = .* )? ) /x;
  1092.         my ($pre, $name) = ($arg =~ /(.*?) \s* \b(\w+) \s* $ /x);
  1093.         next unless length $pre;
  1094.         my $out_type;
  1095.         my $inout_var;
  1096.         if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//) {
  1097.             my $type = $1;
  1098.             $out_type = $type if $type ne 'IN';
  1099.             $arg =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//;
  1100.         }
  1101.         if (/\W/) {    # Has a type
  1102.             push @arg_with_types, $arg;
  1103.             # warn "pushing '$arg'\n";
  1104.             $arg_types{$name} = $arg;
  1105.             $_ = "$name$default";
  1106.         }
  1107.         $only_outlist{$_} = 1 if $out_type eq "OUTLIST";
  1108.         push @outlist, $name if $out_type =~ /OUTLIST$/;
  1109.         $in_out{$name} = $out_type if $out_type;
  1110.         }
  1111.     } else {
  1112.         @args = split(/\s*,\s*/, $orig_args);
  1113.         Warn("Warning: cannot parse argument list '$orig_args', fallback to split");
  1114.     }
  1115.     } else {
  1116.     @args = split(/\s*,\s*/, $orig_args);
  1117.     for (@args) {
  1118.         if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|IN_OUT|OUT)\s+//) {
  1119.         my $out_type = $1;
  1120.         next if $out_type eq 'IN';
  1121.         $only_outlist{$_} = 1 if $out_type eq "OUTLIST";
  1122.         push @outlist, $name if $out_type =~ /OUTLIST$/;
  1123.         $in_out{$_} = $out_type;
  1124.         }
  1125.     }
  1126.     }
  1127.     if (defined($class)) {
  1128.     my $arg0 = ((defined($static) or $func_name eq 'new')
  1129.             ? "CLASS" : "THIS");
  1130.     unshift(@args, $arg0);
  1131.     ($report_args = "$arg0, $report_args") =~ s/^\w+, $/$arg0/;
  1132.     }
  1133.     my $extra_args = 0;
  1134.     @args_num = ();
  1135.     $num_args = 0;
  1136.     my $report_args = '';
  1137.     foreach $i (0 .. $#args) {
  1138.         if ($args[$i] =~ s/\.\.\.//) {
  1139.             $elipsis = 1;
  1140.             if ($args[$i] eq '' && $i == $#args) {
  1141.                 $report_args .= ", ...";
  1142.             pop(@args);
  1143.             last;
  1144.             }
  1145.         }
  1146.         if ($only_outlist{$args[$i]}) {
  1147.         push @args_num, undef;
  1148.         } else {
  1149.         push @args_num, ++$num_args;
  1150.         $report_args .= ", $args[$i]";
  1151.         }
  1152.         if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) {
  1153.             $extra_args++;
  1154.             $args[$i] = $1;
  1155.             $defaults{$args[$i]} = $2;
  1156.             $defaults{$args[$i]} =~ s/"/\\"/g;
  1157.         }
  1158.         $proto_arg[$i+1] = "\$" ;
  1159.     }
  1160.     $min_args = $num_args - $extra_args;
  1161.     $report_args =~ s/"/\\"/g;
  1162.     $report_args =~ s/^,\s+//;
  1163.     my @func_args = @args;
  1164.     shift @func_args if defined($class);
  1165.  
  1166.     for (@func_args) {
  1167.     s/^/&/ if $in_out{$_};
  1168.     }
  1169.     $func_args = join(", ", @func_args);
  1170.     @args_match{@args} = @args_num;
  1171.  
  1172.     $PPCODE = grep(/^\s*PPCODE\s*:/, @line);
  1173.     $CODE = grep(/^\s*CODE\s*:/, @line);
  1174.     # Detect CODE: blocks which use ST(n)= or XST_m*(n,v)
  1175.     #   to set explicit return values.
  1176.     $EXPLICIT_RETURN = ($CODE &&
  1177.         ("@line" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x ));
  1178.     $ALIAS  = grep(/^\s*ALIAS\s*:/,  @line);
  1179.     $INTERFACE  = grep(/^\s*INTERFACE\s*:/,  @line);
  1180.  
  1181.     $xsreturn = 1 if $EXPLICIT_RETURN;
  1182.  
  1183.     # print function header
  1184.     print Q<<"EOF";
  1185. #XS(XS_${Full_func_name})
  1186. #[[
  1187. #    dXSARGS;
  1188. EOF
  1189.     print Q<<"EOF" if $ALIAS ;
  1190. #    dXSI32;
  1191. EOF
  1192.     print Q<<"EOF" if $INTERFACE ;
  1193. #    dXSFUNCTION($ret_type);
  1194. EOF
  1195.     if ($elipsis) {
  1196.     $cond = ($min_args ? qq(items < $min_args) : 0);
  1197.     }
  1198.     elsif ($min_args == $num_args) {
  1199.     $cond = qq(items != $min_args);
  1200.     }
  1201.     else {
  1202.     $cond = qq(items < $min_args || items > $num_args);
  1203.     }
  1204.  
  1205.     print Q<<"EOF" if $except;
  1206. #    char errbuf[1024];
  1207. #    *errbuf = '\0';
  1208. EOF
  1209.  
  1210.     if ($ALIAS) 
  1211.       { print Q<<"EOF" if $cond }
  1212. #    if ($cond)
  1213. #       Perl_croak(aTHX_ "Usage: %s($report_args)", GvNAME(CvGV(cv)));
  1214. EOF
  1215.     else 
  1216.       { print Q<<"EOF" if $cond }
  1217. #    if ($cond)
  1218. #    Perl_croak(aTHX_ "Usage: $pname($report_args)");
  1219. EOF
  1220.  
  1221.     print Q<<"EOF" if $PPCODE;
  1222. #    SP -= items;
  1223. EOF
  1224.  
  1225.     # Now do a block of some sort.
  1226.  
  1227.     $condnum = 0;
  1228.     $cond = '';            # last CASE: condidional
  1229.     push(@line, "$END:");
  1230.     push(@line_no, $line_no[-1]);
  1231.     $_ = '';
  1232.     &check_cpp;
  1233.     while (@line) {
  1234.     &CASE_handler if check_keyword("CASE");
  1235.     print Q<<"EOF";
  1236. #   $except [[
  1237. EOF
  1238.  
  1239.     # do initialization of input variables
  1240.     $thisdone = 0;
  1241.     $retvaldone = 0;
  1242.     $deferred = "";
  1243.     %arg_list = () ;
  1244.         $gotRETVAL = 0;
  1245.  
  1246.     INPUT_handler() ;
  1247.     process_keyword("INPUT|PREINIT|INTERFACE_MACRO|C_ARGS|ALIAS|ATTRS|PROTOTYPE|SCOPE") ;
  1248.  
  1249.     print Q<<"EOF" if $ScopeThisXSUB;
  1250. #   ENTER;
  1251. #   [[
  1252. EOF
  1253.     
  1254.     if (!$thisdone && defined($class)) {
  1255.         if (defined($static) or $func_name eq 'new') {
  1256.         print "\tchar *";
  1257.         $var_types{"CLASS"} = "char *";
  1258.         &generate_init("char *", 1, "CLASS");
  1259.         }
  1260.         else {
  1261.         print "\t$class *";
  1262.         $var_types{"THIS"} = "$class *";
  1263.         &generate_init("$class *", 1, "THIS");
  1264.         }
  1265.     }
  1266.  
  1267.     # do code
  1268.     if (/^\s*NOT_IMPLEMENTED_YET/) {
  1269.         print "\n\tPerl_croak(aTHX_ \"$pname: not implemented yet\");\n";
  1270.         $_ = '' ;
  1271.     } else {
  1272.         if ($ret_type ne "void") {
  1273.             print "\t" . &map_type($ret_type, 'RETVAL') . ";\n"
  1274.                 if !$retvaldone;
  1275.             $args_match{"RETVAL"} = 0;
  1276.             $var_types{"RETVAL"} = $ret_type;
  1277.             print "\tdXSTARG;\n"
  1278.                 if $WantOptimize and $targetable{$type_kind{$ret_type}};
  1279.         }
  1280.  
  1281.         if (@arg_with_types) {
  1282.             unshift @line, @arg_with_types, $_;
  1283.             $_ = "";
  1284.             $processing_arg_with_types = 1;
  1285.             INPUT_handler() ;
  1286.         }
  1287.         print $deferred;
  1288.  
  1289.         process_keyword("INIT|ALIAS|ATTRS|PROTOTYPE|INTERFACE_MACRO|INTERFACE|C_ARGS") ;
  1290.  
  1291.         if (check_keyword("PPCODE")) {
  1292.             print_section();
  1293.             death ("PPCODE must be last thing") if @line;
  1294.             print "\tLEAVE;\n" if $ScopeThisXSUB;
  1295.             print "\tPUTBACK;\n\treturn;\n";
  1296.         } elsif (check_keyword("CODE")) {
  1297.             print_section() ;
  1298.         } elsif (defined($class) and $func_name eq "DESTROY") {
  1299.             print "\n\t";
  1300.             print "delete THIS;\n";
  1301.         } else {
  1302.             print "\n\t";
  1303.             if ($ret_type ne "void") {
  1304.                 print "RETVAL = ";
  1305.                 $wantRETVAL = 1;
  1306.             }
  1307.             if (defined($static)) {
  1308.                 if ($func_name eq 'new') {
  1309.                 $func_name = "$class";
  1310.                 } else {
  1311.                 print "${class}::";
  1312.                 }
  1313.             } elsif (defined($class)) {
  1314.                 if ($func_name eq 'new') {
  1315.                 $func_name .= " $class";
  1316.                 } else {
  1317.                 print "THIS->";
  1318.                 }
  1319.             }
  1320.             $func_name =~ s/^($spat)//
  1321.                 if defined($spat);
  1322.             $func_name = 'XSFUNCTION' if $interface;
  1323.             print "$func_name($func_args);\n";
  1324.         }
  1325.     }
  1326.  
  1327.     # do output variables
  1328.     $gotRETVAL = 0;        # 1 if RETVAL seen in OUTPUT section;
  1329.     undef $RETVAL_code ;    # code to set RETVAL (from OUTPUT section);
  1330.     # $wantRETVAL set if 'RETVAL =' autogenerated
  1331.     ($wantRETVAL, $ret_type) = (0, 'void') if $RETVAL_no_return;
  1332.     undef %outargs ;
  1333.     process_keyword("POSTCALL|OUTPUT|ALIAS|ATTRS|PROTOTYPE"); 
  1334.  
  1335.     &generate_output($var_types{$_}, $args_match{$_}, $_, $DoSetMagic)
  1336.       for grep $in_out{$_} =~ /OUT$/, keys %in_out;
  1337.  
  1338.     # all OUTPUT done, so now push the return value on the stack
  1339.     if ($gotRETVAL && $RETVAL_code) {
  1340.         print "\t$RETVAL_code\n";
  1341.     } elsif ($gotRETVAL || $wantRETVAL) {
  1342.         my $t = $WantOptimize && $targetable{$type_kind{$ret_type}};
  1343.         my $var = 'RETVAL';
  1344.         my $type = $ret_type;
  1345.  
  1346.         # 0: type, 1: with_size, 2: how, 3: how_size
  1347.         if ($t and not $t->[1] and $t->[0] eq 'p') {
  1348.         # PUSHp corresponds to setpvn.  Treate setpv directly
  1349.         my $what = eval qq("$t->[2]");
  1350.         warn $@ if $@;
  1351.  
  1352.         print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n";
  1353.         $prepush_done = 1;
  1354.         }
  1355.         elsif ($t) {
  1356.         my $what = eval qq("$t->[2]");
  1357.         warn $@ if $@;
  1358.  
  1359.         my $size = $t->[3];
  1360.         $size = '' unless defined $size;
  1361.         $size = eval qq("$size");
  1362.         warn $@ if $@;
  1363.         print "\tXSprePUSH; PUSH$t->[0]($what$size);\n";
  1364.         $prepush_done = 1;
  1365.         }
  1366.         else {
  1367.         # RETVAL almost never needs SvSETMAGIC()
  1368.         &generate_output($ret_type, 0, 'RETVAL', 0);
  1369.         }
  1370.     }
  1371.  
  1372.     $xsreturn = 1 if $ret_type ne "void";
  1373.     my $num = $xsreturn;
  1374.     my $c = @outlist;
  1375.     print "\tXSprePUSH;" if $c and not $prepush_done;
  1376.     print "\tEXTEND(SP,$c);\n" if $c;
  1377.     $xsreturn += $c;
  1378.     generate_output($var_types{$_}, $num++, $_, 0, 1) for @outlist;
  1379.  
  1380.     # do cleanup
  1381.     process_keyword("CLEANUP|ALIAS|ATTRS|PROTOTYPE") ;
  1382.  
  1383.     print Q<<"EOF" if $ScopeThisXSUB;
  1384. #   ]]
  1385. EOF
  1386.     print Q<<"EOF" if $ScopeThisXSUB and not $PPCODE;
  1387. #   LEAVE;
  1388. EOF
  1389.  
  1390.     # print function trailer
  1391.     print Q<<EOF;
  1392. #    ]]
  1393. EOF
  1394.     print Q<<EOF if $except;
  1395. #    BEGHANDLERS
  1396. #    CATCHALL
  1397. #    sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason);
  1398. #    ENDHANDLERS
  1399. EOF
  1400.     if (check_keyword("CASE")) {
  1401.         blurt ("Error: No `CASE:' at top of function")
  1402.         unless $condnum;
  1403.         $_ = "CASE: $_";    # Restore CASE: label
  1404.         next;
  1405.     }
  1406.     last if $_ eq "$END:";
  1407.     death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function");
  1408.     }
  1409.  
  1410.     print Q<<EOF if $except;
  1411. #    if (errbuf[0])
  1412. #    Perl_croak(aTHX_ errbuf);
  1413. EOF
  1414.  
  1415.     if ($xsreturn) {
  1416.         print Q<<EOF unless $PPCODE;
  1417. #    XSRETURN($xsreturn);
  1418. EOF
  1419.     } else {
  1420.         print Q<<EOF unless $PPCODE;
  1421. #    XSRETURN_EMPTY;
  1422. EOF
  1423.     }
  1424.  
  1425.     print Q<<EOF;
  1426. #]]
  1427. #
  1428. EOF
  1429.  
  1430.     my $newXS = "newXS" ;
  1431.     my $proto = "" ;
  1432.  
  1433.     # Build the prototype string for the xsub
  1434.     if ($ProtoThisXSUB) {
  1435.     $newXS = "newXSproto";
  1436.  
  1437.     if ($ProtoThisXSUB eq 2) {
  1438.         # User has specified empty prototype
  1439.         $proto = ', ""' ;
  1440.     }
  1441.         elsif ($ProtoThisXSUB ne 1) {
  1442.             # User has specified a prototype
  1443.             $proto = ', "' . $ProtoThisXSUB . '"';
  1444.         }
  1445.         else {
  1446.         my $s = ';';
  1447.             if ($min_args < $num_args)  {
  1448.                 $s = ''; 
  1449.         $proto_arg[$min_args] .= ";" ;
  1450.         }
  1451.             push @proto_arg, "$s\@" 
  1452.                 if $elipsis ;
  1453.     
  1454.             $proto = ', "' . join ("", @proto_arg) . '"';
  1455.         }
  1456.     }
  1457.  
  1458.     if (%XsubAliases) {
  1459.     $XsubAliases{$pname} = 0 
  1460.         unless defined $XsubAliases{$pname} ;
  1461.     while ( ($name, $value) = each %XsubAliases) {
  1462.         push(@InitFileCode, Q<<"EOF");
  1463. #        cv = newXS(\"$name\", XS_$Full_func_name, file);
  1464. #        XSANY.any_i32 = $value ;
  1465. EOF
  1466.     push(@InitFileCode, Q<<"EOF") if $proto;
  1467. #        sv_setpv((SV*)cv$proto) ;
  1468. EOF
  1469.         }
  1470.     } 
  1471.     elsif (@Attributes) {
  1472.         push(@InitFileCode, Q<<"EOF");
  1473. #        cv = newXS(\"$pname\", XS_$Full_func_name, file);
  1474. #        apply_attrs_string("$Package", cv, "@Attributes", 0);
  1475. EOF
  1476.     }
  1477.     elsif ($interface) {
  1478.     while ( ($name, $value) = each %Interfaces) {
  1479.         $name = "$Package\::$name" unless $name =~ /::/;
  1480.         push(@InitFileCode, Q<<"EOF");
  1481. #        cv = newXS(\"$name\", XS_$Full_func_name, file);
  1482. #        $interface_macro_set(cv,$value) ;
  1483. EOF
  1484.         push(@InitFileCode, Q<<"EOF") if $proto;
  1485. #        sv_setpv((SV*)cv$proto) ;
  1486. EOF
  1487.         }
  1488.     }
  1489.     else {
  1490.     push(@InitFileCode,
  1491.          "        ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n");
  1492.     }
  1493. }
  1494.  
  1495. # print initialization routine
  1496.  
  1497. print Q<<"EOF";
  1498. ##ifdef __cplusplus
  1499. #extern "C"
  1500. ##endif
  1501. EOF
  1502.  
  1503. print Q<<"EOF";
  1504. #XS(boot_$Module_cname)
  1505. EOF
  1506.  
  1507. print Q<<"EOF";
  1508. #[[
  1509. #    dXSARGS;
  1510. #    char* file = __FILE__;
  1511. #
  1512. EOF
  1513.  
  1514. print Q<<"EOF" if $WantVersionChk ;
  1515. #    XS_VERSION_BOOTCHECK ;
  1516. #
  1517. EOF
  1518.  
  1519. print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ;
  1520. #    {
  1521. #        CV * cv ;
  1522. #
  1523. EOF
  1524.  
  1525. print @InitFileCode;
  1526.  
  1527. print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ;
  1528. #    }
  1529. EOF
  1530.  
  1531. if (@BootCode)
  1532. {
  1533.     print "\n    /* Initialisation Section */\n\n" ;
  1534.     @line = @BootCode;
  1535.     print_section();
  1536.     print "\n    /* End of Initialisation Section */\n\n" ;
  1537. }
  1538.  
  1539. print Q<<"EOF";;
  1540. #    XSRETURN_YES;
  1541. #]]
  1542. #
  1543. EOF
  1544.  
  1545. warn("Please specify prototyping behavior for $filename (see perlxs manual)\n") 
  1546.     unless $ProtoUsed ;
  1547. &Exit;
  1548.  
  1549. sub output_init {
  1550.     local($type, $num, $var, $init, $name_printed) = @_;
  1551.     local($arg) = "ST(" . ($num - 1) . ")";
  1552.  
  1553.     if(  $init =~ /^=/  ) {
  1554.         if ($name_printed) {
  1555.       eval qq/print " $init\\n"/;
  1556.     } else {
  1557.       eval qq/print "\\t$var $init\\n"/;
  1558.     }
  1559.     warn $@   if  $@;
  1560.     } else {
  1561.     if(  $init =~ s/^\+//  &&  $num  ) {
  1562.         &generate_init($type, $num, $var, $name_printed);
  1563.     } elsif ($name_printed) {
  1564.         print ";\n";
  1565.         $init =~ s/^;//;
  1566.     } else {
  1567.         eval qq/print "\\t$var;\\n"/;
  1568.         warn $@   if  $@;
  1569.         $init =~ s/^;//;
  1570.     }
  1571.     $deferred .= eval qq/"\\n\\t$init\\n"/;
  1572.     warn $@   if  $@;
  1573.     }
  1574. }
  1575.  
  1576. sub Warn
  1577. {
  1578.     # work out the line number
  1579.     my $line_no = $line_no[@line_no - @line -1] ;
  1580.  
  1581.     print STDERR "@_ in $filename, line $line_no\n" ;
  1582. }
  1583.  
  1584. sub blurt 
  1585.     Warn @_ ;
  1586.     $errors ++ 
  1587. }
  1588.  
  1589. sub death
  1590. {
  1591.     Warn @_ ;
  1592.     exit 1 ;
  1593. }
  1594.  
  1595. sub generate_init {
  1596.     local($type, $num, $var) = @_;
  1597.     local($arg) = "ST(" . ($num - 1) . ")";
  1598.     local($argoff) = $num - 1;
  1599.     local($ntype);
  1600.     local($tk);
  1601.  
  1602.     $type = TidyType($type) ;
  1603.     blurt("Error: '$type' not in typemap"), return 
  1604.     unless defined($type_kind{$type});
  1605.  
  1606.     ($ntype = $type) =~ s/\s*\*/Ptr/g;
  1607.     ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
  1608.     $tk = $type_kind{$type};
  1609.     $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/;
  1610.     $type =~ tr/:/_/;
  1611.     blurt("Error: No INPUT definition for type '$type', typekind '$type_kind{$type}' found"), return
  1612.         unless defined $input_expr{$tk} ;
  1613.     $expr = $input_expr{$tk};
  1614.     if ($expr =~ /DO_ARRAY_ELEM/) {
  1615.         blurt("Error: '$subtype' not in typemap"), return 
  1616.         unless defined($type_kind{$subtype});
  1617.         blurt("Error: No INPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return
  1618.             unless defined $input_expr{$type_kind{$subtype}} ;
  1619.     $subexpr = $input_expr{$type_kind{$subtype}};
  1620.     $subexpr =~ s/ntype/subtype/g;
  1621.     $subexpr =~ s/\$arg/ST(ix_$var)/g;
  1622.     $subexpr =~ s/\n\t/\n\t\t/g;
  1623.     $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g;
  1624.     $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/;
  1625.     $expr =~ s/DO_ARRAY_ELEM/$subexpr/;
  1626.     }
  1627.     if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments
  1628.         $ScopeThisXSUB = 1;
  1629.     }
  1630.     if (defined($defaults{$var})) {
  1631.         $expr =~ s/(\t+)/$1    /g;
  1632.         $expr =~ s/        /\t/g;
  1633.         if ($name_printed) {
  1634.           print ";\n";
  1635.         } else {
  1636.           eval qq/print "\\t$var;\\n"/;
  1637.           warn $@   if  $@;
  1638.         }
  1639.         if ($defaults{$var} eq 'NO_INIT') {
  1640.         $deferred .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/;
  1641.         } else {
  1642.         $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t    $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
  1643.         }
  1644.         warn $@   if  $@;
  1645.     } elsif ($ScopeThisXSUB or $expr !~ /^\t\$var =/) {
  1646.         if ($name_printed) {
  1647.           print ";\n";
  1648.         } else {
  1649.           eval qq/print "\\t$var;\\n"/;
  1650.           warn $@   if  $@;
  1651.         }
  1652.         $deferred .= eval qq/"\\n$expr;\\n"/;
  1653.         warn $@   if  $@;
  1654.     } else {
  1655.         die "panic: do not know how to handle this branch for function pointers"
  1656.           if $name_printed;
  1657.         eval qq/print "$expr;\\n"/;
  1658.         warn $@   if  $@;
  1659.     }
  1660. }
  1661.  
  1662. sub generate_output {
  1663.     local($type, $num, $var, $do_setmagic, $do_push) = @_;
  1664.     local($arg) = "ST(" . ($num - ($num != 0)) . ")";
  1665.     local($argoff) = $num - 1;
  1666.     local($ntype);
  1667.  
  1668.     $type = TidyType($type) ;
  1669.     if ($type =~ /^array\(([^,]*),(.*)\)/) {
  1670.         print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1));\n";
  1671.         print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
  1672.     } else {
  1673.         blurt("Error: '$type' not in typemap"), return
  1674.         unless defined($type_kind{$type});
  1675.             blurt("Error: No OUTPUT definition for type '$type', typekind '$type_kind{$type}' found"), return
  1676.                 unless defined $output_expr{$type_kind{$type}} ;
  1677.         ($ntype = $type) =~ s/\s*\*/Ptr/g;
  1678.         $ntype =~ s/\(\)//g;
  1679.         ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
  1680.         $expr = $output_expr{$type_kind{$type}};
  1681.         if ($expr =~ /DO_ARRAY_ELEM/) {
  1682.             blurt("Error: '$subtype' not in typemap"), return
  1683.             unless defined($type_kind{$subtype});
  1684.                 blurt("Error: No OUTPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return
  1685.                     unless defined $output_expr{$type_kind{$subtype}} ;
  1686.         $subexpr = $output_expr{$type_kind{$subtype}};
  1687.         $subexpr =~ s/ntype/subtype/g;
  1688.         $subexpr =~ s/\$arg/ST(ix_$var)/g;
  1689.         $subexpr =~ s/\$var/${var}[ix_$var]/g;
  1690.         $subexpr =~ s/\n\t/\n\t\t/g;
  1691.         $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/;
  1692.         eval "print qq\a$expr\a";
  1693.         warn $@   if  $@;
  1694.         print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic;
  1695.         }
  1696.         elsif ($var eq 'RETVAL') {
  1697.         if ($expr =~ /^\t\$arg = new/) {
  1698.             # We expect that $arg has refcnt 1, so we need to
  1699.             # mortalize it.
  1700.             eval "print qq\a$expr\a";
  1701.             warn $@   if  $@;
  1702.             print "\tsv_2mortal(ST($num));\n";
  1703.             print "\tSvSETMAGIC(ST($num));\n" if $do_setmagic;
  1704.         }
  1705.         elsif ($expr =~ /^\s*\$arg\s*=/) {
  1706.             # We expect that $arg has refcnt >=1, so we need
  1707.             # to mortalize it!
  1708.             eval "print qq\a$expr\a";
  1709.             warn $@   if  $@;
  1710.             print "\tsv_2mortal(ST(0));\n";
  1711.             print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic;
  1712.         }
  1713.         else {
  1714.             # Just hope that the entry would safely write it
  1715.             # over an already mortalized value. By
  1716.             # coincidence, something like $arg = &sv_undef
  1717.             # works too.
  1718.             print "\tST(0) = sv_newmortal();\n";
  1719.             eval "print qq\a$expr\a";
  1720.             warn $@   if  $@;
  1721.             # new mortals don't have set magic
  1722.         }
  1723.         }
  1724.         elsif ($do_push) {
  1725.             print "\tPUSHs(sv_newmortal());\n";
  1726.         $arg = "ST($num)";
  1727.         eval "print qq\a$expr\a";
  1728.         warn $@   if  $@;
  1729.         print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
  1730.         }
  1731.         elsif ($arg =~ /^ST\(\d+\)$/) {
  1732.         eval "print qq\a$expr\a";
  1733.         warn $@   if  $@;
  1734.         print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
  1735.         }
  1736.     }
  1737. }
  1738.  
  1739. sub map_type {
  1740.     my($type, $varname) = @_;
  1741.  
  1742.     $type =~ tr/:/_/;
  1743.     $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s;
  1744.     if ($varname) {
  1745.       if ($varname && $type =~ / \( \s* \* (?= \s* \) ) /xg) {
  1746.     (substr $type, pos $type, 0) = " $varname ";
  1747.       } else {
  1748.     $type .= "\t$varname";
  1749.       }
  1750.     }
  1751.     $type;
  1752. }
  1753.  
  1754.  
  1755. sub Exit {
  1756. # If this is VMS, the exit status has meaning to the shell, so we
  1757. # use a predictable value (SS$_Normal or SS$_Abort) rather than an
  1758. # arbitrary number.
  1759. #    exit ($Is_VMS ? ($errors ? 44 : 1) : $errors) ;
  1760.     exit ($errors ? 1 : 0);
  1761. }
  1762.